package top.lixunda.commerce.common.core.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.Random;

/**
 * 生成图片验证码
 *
 * @author Xumda
 * 2019年12月23日09:20:11
 */
@SuppressWarnings("unused")
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class ImageCodeUtil {

    private int weight = 100;

    private int height = 50;

    private int lineNumber = 6;

    private int charCount = 4;


    private int r = -1;

    private int g = -1;

    private int b = -1;

    private int size = 25;

    private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";

    private static final Random RANDOM = new Random();
    private static final String[] FONT_NAMES = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};

    public void toDefault() {
        this.weight = 100;
        this.height = 50;

        this.lineNumber = 6;
        this.charCount = 4;

        this.r = -1;
        this.g = -1;
        this.b = -1;

        this.size = 25;
    }

    private Color randomColor() {
        if (this.r == -1) {
            this.r = RANDOM.nextInt(100) + 150;
        }
        if (this.g == -1) {
            this.g = RANDOM.nextInt(100) + 150;
        }
        if (this.b == -1) {
            this.b = RANDOM.nextInt(100) + 150;
        }
        return new Color(r, g, b);
    }

    private Font randomFont() {
        int index = RANDOM.nextInt(FONT_NAMES.length);
        String fontName = FONT_NAMES[index];
        int style = RANDOM.nextInt(4);
        size = RANDOM.nextInt(5) + 34;
        //noinspection MagicConstant
        return new Font(fontName, style, size);
    }

    private char randomChar() {
        int index = RANDOM.nextInt(codes.length());
        return codes.charAt(index);
    }

    private void drawLine(BufferedImage image) {
        Graphics2D g = (Graphics2D) image.getGraphics();
        for (int i = 0; i < this.lineNumber; i++) {
            int x1 = RANDOM.nextInt(weight);
            int y1 = RANDOM.nextInt(height);
            int x2 = RANDOM.nextInt(weight);
            int y2 = RANDOM.nextInt(height);
            g.setColor(randomColor());
            g.drawLine(x1, y1, x2, y2);
        }
    }

    private BufferedImage createImage() {
        //创建图片缓冲区
        BufferedImage image = new BufferedImage(weight, height, BufferedImage.TYPE_INT_RGB);
        //获取画笔
        Graphics2D g = (Graphics2D) image.getGraphics();
        //设置背景色
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, weight, height);
        //返回一个图片
        return image;
    }

    /**
     * 获取一张图片验证码图片
     *
     * @return ImageCodeResult
     */
    public ImageCodeResult getImage() {
        BufferedImage image = createImage();
        //获取画笔
        Graphics2D g = (Graphics2D) image.getGraphics();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < this.charCount; i++) {
            //随机生成字符，因为只有画字符串的方法，没有画字符的方法，所以需要将字符变成字符串再画
            String s = randomChar() + "";
            //添加到StringBuilder里面
            sb.append(s);
            //定义字符的x坐标
            float x = i * 1.0F * weight / this.charCount;
            //设置字体，随机
            g.setFont(randomFont());
            //设置颜色，随机
            g.setColor(randomColor());
            g.drawString(s, x, height - (int) (size / 2.0));
        }
        String text = sb.toString();
        drawLine(image);
        return new ImageCodeResult(image, text);
    }

    public static void output(BufferedImage image, OutputStream out) throws IOException {
        ImageIO.write(image, "JPEG", out);
    }

    @SuppressWarnings({"WeakerAccess"})
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    @AllArgsConstructor
    public static class ImageCodeResult {

        private BufferedImage bufferedImage;

        private String text;

        private LocalDateTime time;

        public ImageCodeResult() {
            this.time = LocalDateTime.now();
        }

        public ImageCodeResult(BufferedImage bufferedImage, String text) {
            this(bufferedImage, text, LocalDateTime.now());
        }
    }
}
